/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "ftcontrols.h"
#include "ftpushbutton.h"

FTPushButton::FTPushButton(const QString& s, QWidget *parent) :
    QAbstractButton(parent)
{
    initialize();
    setText(s);
}

FTPushButton::FTPushButton(QWidget *parent) :
    QAbstractButton(parent)
{
    initialize();
}

void FTPushButton::initialize()
{
    setIconSize( QSize(0,0) );
    use_custom_background = false;
    use_custom_foreground = false;
    setAttribute( Qt::WA_NoMousePropagation, true);
    setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
}

QSize FTPushButton::sizeHint() const
{
    int padding = FTControls::defaultPadding()*2;
    int min_height = FTControls::defaultHeight() + padding;
    QFontMetrics fm(font());
    QSize size = fm.size( Qt::TextSingleLine, text() ) +
            QSize( padding,padding ) +
            QSize( iconSize().width(), 0);

    if ( size.width() < min_height )
        size.setWidth( min_height );

    if ( size.height() < iconSize().height() )
        size.setHeight( iconSize().height() );

    if ( size.height() < min_height )
        size.setHeight( min_height );
    return size;
}

QSize FTPushButton::minimumSizeHint() const
{
    int min_height = FTControls::defaultHeight();
    return QSize( min_height, min_height );
}

void FTPushButton::resetCustomBackground()
{
    use_custom_background = false;
    update();
}

void FTPushButton::setCustomBackground( const QBrush& def )
{
    use_custom_background = true;
    background = def;
    update();
}

void FTPushButton::resetCustomForeground()
{
    use_custom_foreground = false;
    update();
}

void FTPushButton::setCustomForeground( const QColor& c)
{
    use_custom_foreground = true;
    foreground = c;
    update();
}

void FTPushButton::paintEvent ( QPaintEvent * /*event*/ )
{
    int padding = FTControls::defaultPadding();
    bool buttonCheck = isDown() || isChecked();
    QPalette palette = QApplication::palette();
    QPainter painter(this);

    // Set default color group
    palette.setCurrentColorGroup( FT_COLOR_GROUP );

    QBrush buttonBrush;
    // Build brushes for background
    // If "custom background" is enabled, just use that!
    // Otherwise, use "Button" or "Highlight" if button is checked/pressed
    if ( use_custom_background )
        buttonBrush = background;
    else
        buttonBrush = palette.brush( buttonCheck ? QPalette::Highlight : QPalette::Button );

    QPen buttonPen;
    // Build pens needed for the button
    // The "border" shall be either the custom one
    // Otherwise, use "ButtonText" or "HighlightText" if button is checked/pressed
    if ( use_custom_foreground )
        buttonPen = foreground;
    else
        buttonPen = palette.color( buttonCheck ? QPalette::HighlightedText : QPalette::ButtonText );

    QPen borderPen;
    // Build pens needed for the button
    // The "border" shall be either the custom one
    // Otherwise, use "ButtonText" or "HighlightText" if button is checked/pressed
    borderPen = palette.color( QPalette::Light );

    // Shorthand for the button overall rect:
    QRect areaRect = rect().adjusted(padding,padding,-padding,-padding);
    // This is the "rect" of the border
    QRect borderRect = areaRect.adjusted(padding,padding,-padding,-padding);

    // Fill up the button background with our brush...
    painter.fillRect( areaRect, buttonBrush );

    // Set default pen for the border and draw the border
    painter.setPen( borderPen );
    painter.drawRect( borderRect );

    painter.setPen( buttonPen );

    bool hasIcon = !icon().isNull();
    bool hasText = text() != "";

    // If there is an icon...
    if ( hasIcon )
        // Paint the icon!
        icon().paint( &painter, areaRect.adjusted( 3,3,-3,-3 ), (hasText ? Qt::AlignLeft : Qt::AlignCenter) | Qt::AlignVCenter);

    // If there is some text to print...
    if ( hasText )
    {
        QRect textRect = borderRect.adjusted( iconSize().width()+1, 1, 1, 1);
        // Draw the text
        painter.drawText( textRect,Qt::AlignCenter, text() );
    }
}
